package com.ratchet.ShotShakr;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import android.content.Context;
import android.util.Log;
import android.widget.TextView;

public class ShotUpdateHelper {

	private static final String GETSHOTS_SOAP_ACTION = "http://tempuri.org/GetAllShots";
	private static final String GETHAMMS_SOAP_ACTION = "http://tempuri.org/GetAllHamms";
	private static final String GETCOUNT_SOAP_ACTION = "http://tempuri.org/ShotCount";
	private static final String GETSHOTS_METHOD_NAME = "GetAllShots";
	private static final String GETHAMMS_METHOD_NAME = "GetAllHamms";
	private static final String GETCOUNT_METHOD_NAME = "ShotCount";
	private static final String GETHAMMSCOUNT_METHOD_NAME = "HammsCount";
	private static final String NAMESPACE = "http://tempuri.org";
	private static final String URL = "http://www.shotshakr.com/Service/ShotsService.asmx";
	
	private int _updateStatus;
	
	TextView tv;
	JSONArray json;

	ShotRepository repo;
	
	
	public void setUpdateStatus(int value){
		_updateStatus = value;
	}
	public int getUpdateStatus(){
		return _updateStatus;
	}
	
	public void GetShotUpdate(Context context) {

		SoapObject Request = new SoapObject(NAMESPACE, GETSHOTS_METHOD_NAME);

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.setOutputSoapObject(Request);

		AndroidHttpTransport aht = new AndroidHttpTransport(URL);

		SoapPrimitive resultString;
		try {
			aht.call(GETSHOTS_SOAP_ACTION, soapEnvelope);
			resultString = (SoapPrimitive) soapEnvelope.getResponse();

			json = new JSONArray(resultString.toString());

			this.repo = new ShotRepository(context); 
			this.repo.deleteAll();
			
			//perform insert
			for (int i = 0; i < json.length(); i++) {				 
				JSONObject shot = new JSONObject();
				shot = json.getJSONObject(i);
				Log.w("UpdateShots","Updating Shot.... " + shot.getString("shot_name"));
				this.repo.insert(shot.getString("shot_name"),shot.getString("ingredients"));				
				setUpdateStatus(i + 1);
			}
			
			

		} catch (Exception e) {
			Log.w("ServiceCall", e.getMessage());
		}
		this.repo.close();
	}
	
	public void GetHammsUpdate(Context context) {

		SoapObject Request = new SoapObject(NAMESPACE, GETHAMMS_METHOD_NAME);

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.setOutputSoapObject(Request);

		AndroidHttpTransport aht = new AndroidHttpTransport(URL);

		SoapPrimitive resultString;
		try {
			aht.call(GETHAMMS_SOAP_ACTION, soapEnvelope);
			resultString = (SoapPrimitive) soapEnvelope.getResponse();

			json = new JSONArray(resultString.toString());

			this.repo = new ShotRepository(context); 
			this.repo.deleteAll();
			
			//perform insert
			for (int i = 0; i < json.length(); i++) {				 
				JSONObject shot = new JSONObject();
				shot = json.getJSONObject(i);
				Log.w("UpdateHamms","Updating Hamms.... " + shot.getString("shot_name"));
				this.repo.insert(shot.getString("shot_name"),shot.getString("ingredients"));				
				setUpdateStatus(i + 1);
			}
			
			this.repo.close();

		} catch (Exception e) {
			Log.w("ServiceCall", e.getMessage());
		}
		
	}
	
	public Boolean hasUpdate(Context context){
		
		this.repo = new ShotRepository(context); 
		int DBitems =  this.repo.getAllShots().size();
		int myCount = GetShotCount(context);
		Log.w("hasUpdate", "DBItems = " + DBitems + "myCOunt = " + myCount);
		if ((myCount > 0) && (myCount > DBitems)){
			return true;
		}
		return false; 
			
	}
	public int GetShotCount(Context context) {
		int items = 0;
		
		SoapObject Request = new SoapObject(NAMESPACE, GETCOUNT_METHOD_NAME);

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.setOutputSoapObject(Request);

		AndroidHttpTransport aht = new AndroidHttpTransport(URL);

		SoapPrimitive resultString;
		try {
			aht.call(GETCOUNT_SOAP_ACTION, soapEnvelope);
			resultString = (SoapPrimitive) soapEnvelope.getResponse();
			Log.w("GetShotCount","Number of shots in the Service = " +  resultString.toString());
			items = Integer.parseInt(resultString.toString());
			

			
				
			
		

		} catch (Exception e) {
			Log.w("ServiceCall", "Error getting shot count");
		}
		
		return items;
	}
	

	

}
